const { htmlToDocx } = require('../dist/lib/index.js');

async function testImageSize() {
  // 解析你的 base64 图片数据
  const base64Data = 'iVBORw0KGgoAAAANSUhEUgAABxEAAAAGCAYAAAAMhWBaAAAAAXNSR0IArs4c6QAAAARzQklUCAgICHwIZIgAAABWSURBVHic7dmhAQAgEMSwg/13fgzmJgCRTFDfNckEAAAAAAAA4NqvAwAAAAAAAIC/mIgAAAAAAABAMREBAAAAAACAYiICAAAAAAAAxUQEAAAAAAAAygGbJQIKWhAyGAAAAABJRU5ErkJggg==';

  console.log('解析 base64 图片数据...');
  const data = Buffer.from(base64Data, 'base64');
  console.log('图片数据大小:', data.length, '字节');

  // 尝试解析 PNG 尺寸
  if (data.length >= 24) {
    const width = data.readUInt32BE(16);
    const height = data.readUInt32BE(20);
    console.log('解析到的图片尺寸:', width, 'x', height);
  }

  const html = `
    <img style="display: inline-block; height: 2px;" src="data:image/png;base64,${base64Data}" width="100%" />
  `;

  try {
    console.log('\n开始转换 HTML 到 DOCX...');
    const result = await htmlToDocx(html);
    console.log('转换成功！');
    console.log('警告信息:', result.warnings);

    // 检查文档内容
    const documentWrapper = result.document.documentWrapper;
    const document = documentWrapper.document;
    const body = document.body;

    body.root.forEach((item, index) => {
      if (item.constructor.name === 'Paragraph') {
        item.root.forEach((rootItem) => {
          if (rootItem.constructor.name === 'ImageRun') {
            console.log('找到图片运行！');

            if (rootItem.root) {
              rootItem.root.forEach((rootChild) => {
                if (rootChild.constructor.name === 'Drawing') {
                  console.log('Drawing 元素 root 长度:', rootChild.root.length);
                  rootChild.root.forEach((drawingChild, childIndex) => {
                    console.log(`  Drawing 子元素 ${childIndex}:`, drawingChild.constructor.name);
                    if (drawingChild.constructor.name === 'Inline') {
                      console.log('  找到 Inline 元素！');
                      console.log('  Inline 属性:', Object.keys(drawingChild));
                    }
                  });
                }
              });
            }
          }
        });
      }
    });
  } catch (error) {
    console.error('转换失败:', error);
  }
}

testImageSize();
